package org.dru.clay.util.file;

import org.dru.clay.util.functional.Predicate;

import java.io.File;

/**
 * FilePredicates
 * User: joakimd
 * Date: 2013-07-24
 * Time: 13:20
 */
public final class FilePredicates {
    private FilePredicates() {
    }

    public static Predicate<File> exists() {
        return FilePredicate.EXISTS;
    }

    public static Predicate<File> isFile() {
        return FilePredicate.IS_FILE;
    }

    public static Predicate<File> isDirectory() {
        return FilePredicate.IS_DIRECTORY;
    }

    public static Predicate<File> isRelativeOf(final File dir) {
        return new IsRelativeOfPredicate(dir);
    }

    private static enum FilePredicate implements Predicate<File> {
        EXISTS {
            @Override
            public boolean apply(final File value) {
                return value.exists();
            }
        },
        IS_FILE {
            @Override
            public boolean apply(final File value) {
                return EXISTS.apply(value) && value.isFile();
            }
        },
        IS_DIRECTORY {
            @Override
            public boolean apply(final File value) {
                return EXISTS.apply(value) && value.isDirectory();
            }
        }
    }

    private static class IsRelativeOfPredicate implements Predicate<File> {
        private final File dir;

        private IsRelativeOfPredicate(final File dir) {
            dir.getClass();
            this.dir = dir;
        }

        @Override
        public boolean apply(final File value) {
            return FileUtils.isRelative(dir, value);
        }
    }
}
